//
//  WithdrawViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/7.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "WithdrawViewController.h"
#import "BuresResultViewController.h"//充值和提现结果页
#import "LMPopInputPasswordView.h"
#import "ZJSecuritycodeViewController.h"//找回密码
#import "AccountViewController.h"
@interface WithdrawViewController ()<LMPopInputPassViewDelegate>
{
    LMPopInputPasswordView *popView;
}
@property(nonatomic ,strong)UILabel *countLabel;
@property(nonatomic ,strong)NSString *bankId;
@property(nonatomic ,strong)UITextField *applyField;

@end

@implementation WithdrawViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self showNav];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"钱包提现");
    // Do any additional setup after loading the view.
}

-(void)createView{
    //一
    UIView*view=[[UIView alloc]init];
    view.frame=CGRectMake(0, 0, MainW, 50);
    view.backgroundColor=Gray_Color;
    [self.view addSubview:view];
    MYLabel *titleLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, MainW-20, 20)];
    titleLabel.textColor=TextColor6;
    titleLabel.text=[NSString stringWithFormat:@"%@%@(元)",MyString(@"钱包可用金额"),self.dic[@"availableBalance"]];
    [view addSubview:titleLabel];
    
    
    
    //二
    UIView*view1=[[UIView alloc]init];
    view1.frame=CGRectMake(0, 50, MainW, 50);
    view1.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view1];
    MYLabel *MLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, 70, 20)];
    MLabel.textColor=TextColor3;
    //MLabel.backgroundColor=[UIColor redColor];
    MLabel.text=[NSString stringWithFormat:@"%@(元)",MyString(@"金额")];
    UITextField*field=[[UITextField alloc]initWithFrame:CGRectMake(MLabel.frame.size.width+35, 15, MainW-MLabel.frame.size.width-35, 20)];
    field.placeholder=MyString(@"请填写提现金额");
    field.borderStyle=0;
    self.applyField=field;
    field.keyboardType=UIKeyboardTypeNumberPad;
    field.font=FontAndStyle14;
    [view1 addSubview:field];
    [view1 addSubview:MLabel];
    
    
    
    //三
    UIView*view2=[[UIView alloc]init];
    view2.frame=CGRectMake(0, 100, MainW, 50);
    view2.backgroundColor=Gray_Color;
    [self.view addSubview:view2];
    MYLabel *TLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, MainW-20, 20)];
    TLabel.textColor=TextColor6;
    TLabel.text=MyString(@"提现至");
    [view2 addSubview:TLabel];
    
    
    
    //四
    UIView*view3=[[UIView alloc]init];
    view3.frame=CGRectMake(0, 150, MainW, 50);
    view3.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view3];
    MYLabel *ZLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, 60, 20)];
    ZLabel.textColor=TextColor3;
    //ZLabel.backgroundColor=[UIColor redColor];
    ZLabel.text=[NSString stringWithFormat:@"%@",MyString(@"账户")];
    MYLabel *HLabel=[[MYLabel alloc]initWithFrame:CGRectMake(MLabel.frame.size.width+35, 15, MainW-27-MLabel.frame.size.width-35, 20)];
    HLabel.textColor=TextColor6;
    //ZLabel.backgroundColor=[UIColor redColor];
    HLabel.text=@"选择账户";
    HLabel.textAlignment=2;
    self.countLabel=HLabel;
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-22, 16, 10, 18)];
    img.image=[UIImage imageNamed:@"icon_arrow_right"];
    [view3 addSubview:HLabel];
    [view3 addSubview:img];
    [view3 addSubview:ZLabel];
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 150, MainW, 50)];
    button.backgroundColor=[UIColor clearColor];
    [button addTarget:self action:@selector(ChooseCount) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    //确认提现按钮
    UIButton *Surebtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 230, MainW-20, 50)];
    Surebtn.backgroundColor=Red_Color;
    [Surebtn setTitle:MyString(@"确认提现") forState:UIControlStateNormal];
    [Surebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [Surebtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    ViewRadius(Surebtn, 5);
    [self.view addSubview:Surebtn];
    UILabel *Blabel=[[UILabel alloc]init];
    Blabel.text=MyString(@"备注");
    Blabel.font=FontAndStyle12;
    CGSize size=[Blabel.text sizeWithAttributes:@{NSFontAttributeName:Blabel.font }];
    Blabel.textColor=HJ_Color(207, 0, 0);;
    Blabel.frame=CGRectMake(10, 290, size.width, 20);
    [self.view addSubview:Blabel];
    UILabel *Alabel=[[UILabel alloc]init];
    Alabel.text=MyString(@"备注一");
    Alabel.textColor=HJ_Color(207, 0, 0);
    Alabel.frame=CGRectMake(size.width+10, 290, MainW-80, 20);
    [self.view addSubview:Alabel];
    UILabel *Clabel=[[UILabel alloc]init];
    Clabel.text=MyString(@"备注二");
    Clabel.textColor=HJ_Color(207, 0, 0);
    Clabel.frame=CGRectMake(size.width+10, 310, MainW-80, 20);
    [self.view addSubview:Clabel];
    Alabel.font=FontAndStyle12;
    Clabel.font=FontAndStyle12;
    
    NSLog(@"%@",Clabel.font);

    
}
-(void)ChooseCount{
    AccountViewController*vc=[[AccountViewController alloc]init];
    vc.chooseBank = ^(NSDictionary *bankInfo) {
        NSString *bank=bankInfo[@"creditAccount"];
        NSString*newStr=[bank substringWithRange:NSMakeRange(bank.length-4,4)];
        self.countLabel.text=[NSString stringWithFormat:@"%@(%@)",bankInfo[@"creditBank"],newStr];
        self.bankId=bankInfo[@"bankAccountId"];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)sureClick{
    NSLog(@"确认提现");
    if (self.applyField.text.length==0) {
        [self showToast:MyString(@"请填写提现金额")];
        return;
    }
    if ([self.dic[@"availableBalance"] intValue]==0) {
        [self showToast:MyString(@"暂无余额")];
        return;
    }
    if ([self.countLabel.text isEqualToString:@"选择账户"]) {
        [self showToast:MyString(@"请选择账户")];
        return;
    }
    if ([self.applyField.text intValue]<=200) {
        [self showToast:MyString(@"金额低于最低限制!")];
        return;
    }
    NSLog(@"确认提现");
    popView = [[LMPopInputPasswordView alloc]init];
    popView.frame = CGRectMake((self.view.frame.size.width - 255)*0.5, 50, 255, 150);
    popView.delegate = self;
    [popView pop];

}
#pragma mark ---LMPopInputPassViewDelegate
-(void)buttonClickedAtIndex:(NSUInteger)index withText:(NSString *)text
{
    NSLog(@"buttonIndex = %li password=%@",(long)index,text);
    if(index==1){
        if(text.length==0){
            NSLog(@"密码长度不正确");
        }else if(text.length<6){
            NSLog(@"密码长度不正确");
        }else{
            NSLog(@"输出的密码:%@",text);
            [self loadApplyRequest:text];
        }
    }else{
        NSLog(@"老子忘记密码了");
    }
}
-(void)buttonClickedForgetpassWord{
    ZJSecuritycodeViewController*vc=[[ZJSecuritycodeViewController alloc]init];
    vc.binkCart=2;
    vc.curphoneStr=[ZJUserDefaultsTools getCurrentUserPhone];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"忘记密码了");
}
-(void)deviceOrientationDidChange:(NSObject*)sender{
    UIDevice* device = [sender valueForKey:@"object"];
    if(device.orientation==UIInterfaceOrientationLandscapeLeft||device.orientation==UIInterfaceOrientationLandscapeRight)
    {
        popView.frame = CGRectMake((self.view.frame.size.width - 250)*0.5, 50, 250, 130);
    }
    else if(device.orientation==UIInterfaceOrientationPortrait||device.orientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        popView.frame = CGRectMake((self.view.frame.size.width - 250)*0.5, 50, 250, 130);
    }
}
-(void)loadApplyRequest:(NSString*)password{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"cashOrderType"]=@"bank";
    dic[@"bankAccountId"]=self.bankId;
    dic[@"amount"]=self.applyField.text;
    dic[@"password"]=password;
    [self doHttpRequestWithApi:APPLYCASH method:GET dic:dic successBlock:^(NSDictionary *dic) {
        //[self dismissDialog];
        [MBProgressHUD showSuccess:MyString(@"申请成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}


@end
